<template>
	<view>
		<u-loading-page :loading="loading"></u-loading-page>
		<view class="page">
			<u-grid :border="false"	>
					<u-grid-item v-for="(item, index) in showCardList" :key="index" @click="gridClick(item)">
					<view class='card'>
						<image :src="item.img" class="service-img-style"></image>
						<view>{{item.title}}</view>
					</view>
				</u-grid-item>
			</u-grid>
		</view>
	</view>
</template>

<script>
	import {navigateToMy, showInfo, showModalPromise, showModal} from "@/util/uniUtils";
	import {userInfoMixins} from "@/mixins/userInfoMixins"
	import {userType} from "@/enum/userTypeEnum";

	let pathPrefix = '../../static/tabbar/'
	export default {
		mixins: [userInfoMixins],
		name: "home",
		data() {
			return {
				cardList: [{
					id: 1,
					title: '打印合格证',
					img: pathPrefix + 'print.png',
					toLink: '/subPackages/home/print/printForm'
				},
					{
						id: 2,
						title: '打印列表',
						img: pathPrefix + 'icon_print_list.png',
						toLink: '/subPackages/home/certificate/certificateList'
					},
					{
						id: 3,
						title: '工作台',
						img: pathPrefix + 'home/works.png',
						toLink: '/subPackages/home/record/workbench'
					},
					{
						id: 4,
						title: '检查记录',
						img: pathPrefix + 'icon_print_list.png',
						toLink: '/subPackages/home/record/checkRecordList'
					},
					{
						id: 5,
						title: '打印详情',
						img: pathPrefix + 'print.png',
						toLink: '/subPackages/home/certificate/certificateList'
					},
					{
						id: 6,
						title: '开具证明',
						img: pathPrefix + 'home/sum.png',
						toLink: '/subPackages/home/certificate/certificateCountList'
					},
					{
						id: 7,
						title: '会员信息',
						img: pathPrefix + 'home/vip.png',
						toLink: '/subPackages/home/user/userPerfectList'
					}
				],
				// 根据角色判断后最终需要展示的卡片
				showCardList: [],
				loading: true,
			}
		},
		onShow() {
			this.init();
		},
		methods: {
			/**
			 * 初始化方法
			 */
			async init() {
				// 先置空能看到的卡片
				this.showCardList = []
				this.loading = true
				// 查询当前登录用户信息，使用 userInfoMixins 里的方法
				let result = await this.getUserInfo()
				if (result && !this.sysUser) {
					showInfo("用户信息查询失败")
					this.loading = false
					return;
				}

				// 用户类型
				let sysUserType = this.sysUser.user.roles[0].roleKey
				console.log(sysUserType)
				if (userType.REGULATOR.code === sysUserType || userType.ADMIN.code === sysUserType) {
					// 监管者或管理人员，定义其能看到的列表
					let list = [3, 4, 5, 6, 7]
					this.showCardList = this.cardList.filter(t => list.includes(t.id))
				} else if (userType.COMMON.code === sysUserType) {
					// 普通用户，能看到的列表
					let list = [1, 2]
					this.showCardList = this.cardList.filter(t => list.includes(t.id))
				} else {
					this.showCardList = []
					showInfo("用户角色信息有误")
				}
				this.loading = false
			},

			/**
			 * 卡片点击事件
			 *
			 * @param value
			 */
			gridClick(item) {
				let {id, toLink} = item
				switch (id) {
					case 1:
					case 2:
						// 判断是否是会员
						if (!this.userRegister) {
							showInfo("会员信息有误");
							return
						}
						// 未完善资料
						if (this.userRegister.perfectFlag !== 1) {
							showModalPromise("您还不是会员，资料完善后即可成为会员，是否立即完善？").then(flag => {
								if (flag) {
									navigateToMy('/subPackages/user/perfect')
								}
							})
							return;
						}
						// 已完善资料，但未审核
						if (this.userPerfect && this.userPerfect.examineStatus === 0) {
							showModal("您提交的信息正在审核中，请耐心等待，审核通过后即可成为会员")
							return;
						}
						// 如果信息完善后，审核不通过，给出提示
						if (this.userPerfect && this.userPerfect.examineStatus === 1) {
							let content = `您提交的资料未审核通过，是否从新提交？原因【${this.userPerfect.examineMsg}】`
							showModalPromise(content).then(flag => {
								if (flag) {
									navigateToMy('/subPackages/user/perfect')
								}
							})
							return;
						}
						if (toLink) {
							navigateToMy(toLink)
						}
						break;
					case 3:
					case 4:
					case 5:
					case 6:
					case 7:
						if (toLink) {
							navigateToMy(toLink)
						}
						break;
					default:
						break;
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background-color: #efefef;
		height: 100vh;
	}

	.card {
		width: 200rpx;
		height: 200rpx;
		border-radius: 10px;
		background-color: white;
		text-align: center;
		margin-top: 10px;
	}

	.service-img-style {
		width: 85rpx;
		height: 85rpx;
		margin-top: 30rpx;
		margin-bottom: 15rpx;
	}
</style>
